package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduCourseDescription;
import com.atguigu.eduservice.entity.vo.CourseInfoVo;
import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.mapper.EduCourseMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduCourseDescriptionService;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author lsx
 * @since 2020-08-01
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
    //课程描述注入
    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;

    //注入小节的service
    @Autowired
    private EduVideoService eduVideoService;
    //注入章节的service
    @Autowired
    private EduChapterService eduChapterService;

    @Override//保存课程信息 将VO中的值分别存到数据库课程表和课程简介表中
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        //将vo对象中的数据转换到eduCourse
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        //将eduCourse中的数据保存到了对应的表中
        int insert = baseMapper.insert(eduCourse);
        if (insert<=0){
            throw  new GuliException(20001,"执行了全局异常");
        }
        String cid = eduCourse.getId();
        //2.将eduCourseDescription中的数据保存到它所对应的表中 注入eduCourseDescriptionService即可
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        //课程表与课程描述表是一对一的关系  将课程id设置到课程描述表中 这样就建立了联系
        eduCourseDescription.setId(cid);
        eduCourseDescriptionService.save(eduCourseDescription);
        return  cid;

    }

    /**
     * 根据课程id获取课程信息
     * 注:这里时查询得到VO对应进行回显
     * @param courseId
     * @return
     */
    @Override
    public CourseInfoVo getCourseById(String courseId) {
        //CourseInfoVo对象中的内容涉及两个表 所以要查询两张表的内容 复制给CourseInfoVo对象
        EduCourse eduCourse = baseMapper.selectById(courseId);
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(eduCourse,courseInfoVo);

        EduCourseDescription eduCourseDescription = eduCourseDescriptionService.getById(courseId);
        courseInfoVo.setDescription(eduCourseDescription.getDescription());

        return courseInfoVo;
    }

    /**
     * 更新课程信息
     * @param courseInfoVo
     */
    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        String id = courseInfoVo.getId();
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        baseMapper.updateById(eduCourse);

        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        BeanUtils.copyProperties(courseInfoVo,eduCourseDescription);
        eduCourseDescriptionService.updateById(eduCourseDescription);
    }

    @Override
    public CoursePublishVo getCoursePublishInfo(String id) {
        CoursePublishVo publishCourseInfo = baseMapper.getPublishCourseInfo(id);
        return publishCourseInfo;
    }
    //删除课程
    @Override
    public void removeCourse(String courseId) {
        //1.根据课程id 删除小节
        eduVideoService.deleteVideoByCourseId(courseId);
        //2.根据课程id删除章节
        eduChapterService.deleteChapterByCourseId(courseId);

        //3.根据课程id删除描述
        eduCourseDescriptionService.removeById(courseId);

        //4.根据课程id删除课程本身
        int result = baseMapper.deleteById(courseId);

        if(result == 0){//失败返回
            throw  new GuliException(20001,"删除失败");
        }

    }
    //查询前8名热门课程
    @Cacheable(value = "course",key = "'getHotCourse'")
    @Override
    public List<EduCourse> getHotCourse() {
        QueryWrapper<EduCourse> wrapperCourse = new QueryWrapper<>();
        //这里根据id进行排序 按照公司业务 确定以那个字段进行排序
        wrapperCourse.orderByDesc("id");
        wrapperCourse.last("limit 8");
        List<EduCourse> courseList = baseMapper.selectList(wrapperCourse);
        return courseList;
    }


}
